﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Animaonline.Globals;
using Animaonline.Weather.WeatherData;


namespace Weather
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //TODO: use error handling where an error for a city doesn't stop processing for rest of cities

            try
            {
                //get list of cities from XML file
                List<City> cityList = new List<City>();
                XElement CitiesFile = XElement.Load(Server.MapPath("~/App_Data/Cities.xml"));
                var Cities = (from city in CitiesFile.Elements("City")
                              from name in city.Elements("Name")
                              from temperatureType in city.Elements("TemperatureType")
                              select new {CityName = name.Value, TemperatureType = temperatureType.Value});


                foreach (var city in Cities)
                {
                    GoogleWeatherData weatherData =
                        Animaonline.Weather.GoogleWeatherAPI.GetWeather(LanguageCode.en_US, city.CityName);

                    City newCity = new City();

                    //remove state part
                    newCity.Name = city.CityName.IndexOf(",") > -1 ? city.CityName.Remove(city.CityName.IndexOf(",")) : city.CityName;
                    newCity.TemperatureType = city.TemperatureType;

                    if (city.TemperatureType == "F")
                    {
                        newCity.LowTemp = (int) weatherData.ForecastConditions[0].Low.Fahrenheit;
                        newCity.HighTemp = (int) weatherData.ForecastConditions[0].High.Fahrenheit;
                        newCity.CurrentTemperatureForDisplay =
                            (int) weatherData.CurrentConditions.Temperature.Fahrenheit + "&deg;F";
                    }
                    else if (city.TemperatureType == "C")
                    {
                        newCity.LowTemp = (int) weatherData.ForecastConditions[0].Low.Celsius;
                        newCity.HighTemp = (int) weatherData.ForecastConditions[0].High.Celsius;
                        newCity.CurrentTemperatureForDisplay = (int) weatherData.CurrentConditions.Temperature.Celsius +
                                                               "&deg;C";
                    }

                    newCity.Humidity = weatherData.CurrentConditions.Humidity.Replace("Humidity:", "").Trim();

                    string iconFileName = Path.GetFileName(weatherData.CurrentConditions.Icon);
                    newCity.IconPath = "images/" + iconFileName;

                    cityList.Add(newCity);
                }

                rptCity.DataSource = cityList;
                rptCity.DataBind();
            }
            catch (Exception)
            {
                ltrMessage.Text = "An error occured. Please try again or report problem to webmaster.";
            }
        }

        //Was to be used to test if we have a valid temperature value
        private bool IsNumeric(string Expression)
        {
            bool isNum;
            double retNum;
            isNum = Double.TryParse(Expression, System.Globalization.NumberStyles.Any,
                                    System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
            return isNum;
        }
    }
}